package com.atguigu.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

public class ZListValueConstraintValidator implements ConstraintValidator<ZListValue, Integer> {

    Set<Integer> valueSet = new HashSet<>();

    @Override
    public void initialize(ZListValue constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
        if (constraintAnnotation == null || constraintAnnotation.vals() == null) {
            return;
        }

        for (int val : constraintAnnotation.vals()) {
            valueSet.add(val);
        }
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return valueSet.contains(value);
    }
}
